<template>
  <div ref="BarECharts" style="width: 100%;height: 100%"></div>
</template>

<script setup>
import {nextTick, ref, watch} from "vue";
import * as echarts from 'echarts';

const BarECharts = ref(null)

const props = defineProps(['optionsData'])

watch(() => props.optionsData, (newVal) => {
  if (newVal) {
    nextTick(() => {
      init()
    })
  }
}, {deep: true, immediate: true})

const init = () => {
  if (BarECharts.value) {
    const Echarts = echarts.init(BarECharts.value);
    const option = {
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'cross',
          label: {
            backgroundColor: '#6a7985'
          }
        }
      },
      xAxis: {
        type: 'category',
        data: props.optionsData.lables
      },
      yAxis: {
        type: 'value'
      },
      grid: {
        top: '10%',
        left: '10%',
        right: '4%',
        bottom: '10%',
        containLabel: true
      },
      series: [
        {
          data: props.optionsData.values,
          type: 'bar',
          barWidth: 30,
          showBackground: true,
          backgroundStyle: {
            color: 'rgba(180, 180, 180, 0.2)'
          },
          itemStyle: {
            normal: {
              color: '#528dff',
            },
          },
        }
      ]
    };
    Echarts.setOption(option);
    window.onresize = Echarts.resize
  }
}


</script>

<style lang="less" scoped>
</style>
